//
//  SearchVaccinController.m
//  Cabinet
//
//  Created by Florent Montsegur on 23/08/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "SearchVaccinController.h"

@implementation SearchVaccinController

@synthesize mVaccinsView;
@synthesize mManagedObjectContext;
@synthesize mVaccinationEntity;

- (id)initWithWindow:(NSWindow *)window
{
    self = [super initWithWindow:window];
    if (self) {
        // Initialization code here.
        mVaccins = [[NSMutableArray alloc] init];
    }
    
    return self;
}

- (void)windowDidLoad
{
    [super windowDidLoad];
    
    // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
}

- (void)dealloc
{
    if(mVaccins != nil)
        [mVaccins removeAllObjects];
}

- (NSString*)windowNibName
{
	return @"SearchVaccin";
}


- (void)withParentWindow:parentWindow
{
    mSelectedIndex = -1;
	NSWindow* window = [self window];
	
	[NSApp beginSheet:window modalForWindow:parentWindow modalDelegate:nil didEndSelector:nil contextInfo:nil];
	[NSApp runModalForWindow:window];
	// sheet is up now...
	
	[NSApp endSheet:window];
	[window orderOut:self];
}


- (IBAction)okAction:sender {
    
    NSLog(@"On OK");
    
    mSelectedIndex = [mVaccinsView selectedRow];
    
    mResult = NSOKButton;
    [self closeWindow];
}

- (IBAction)annulerAction:sender {
    
    NSLog(@"On Annuler");
    
    mResult = NSCancelButton;
    [self closeWindow];
}

- (void) closeWindow
{
    [[self mVaccinsView] setDataSource:nil];
    [[self window] orderOut:nil];
    [NSApp stopModal];    
}

- (int) returnCode
{
    return mResult;
}

- (void) setVaccins:(NSArray*)aVaccinsList
{
    [mVaccins removeAllObjects];
    
    if(aVaccinsList == nil)
        return;
    
    [mVaccins addObjectsFromArray:aVaccinsList];
    [mVaccinsView reloadData];
}

- (Vaccin*) selectedVaccin
{
    return [self getVaccinAtIndex:mSelectedIndex];
}

- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView{
    return [self count];
}

- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex{
    
    if( rowIndex >= [self count])
        return @"";
    
    Vaccin *vac = [mVaccins objectAtIndex:rowIndex];
    if(vac !=nil)
        return [vac Nom];
    
    return @"";
}

- (NSInteger) count{
    return [mVaccins count];
}

- (Vaccin *)getVaccinAtIndex:(NSInteger)aRowIndex
{
    if(aRowIndex<0) 
        return nil;
    
    if(aRowIndex>=[self count]) 
        return nil;
    
    return [mVaccins objectAtIndex:aRowIndex];
}

- (IBAction)supprimerAction:sender
{
    Vaccin * selectedVac = [self selectedVaccin];
    if(selectedVac == nil)
        return;
    
    NSFetchRequest *request = [[NSFetchRequest alloc] init] ;
    [request setEntity:mVaccinationEntity];
    
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"Vaccin == %@", selectedVac];
    [request setPredicate:predicate];
    
    NSArray * result = [[self mManagedObjectContext] executeFetchRequest:request error:nil];
    if([result count]<=0)
    {
        NSAlert * alert = [NSAlert alertWithMessageText:@"Attention"
                                          defaultButton:@"Supprimer"
                                        alternateButton:@"Annuler"
                                            otherButton:nil informativeTextWithFormat:[NSString stringWithFormat:@"%@ sera supprimé définitivement", [selectedVac Nom]] ];
        
        // should we try again?
        if ([alert runModal] == NSOKButton)
        {
            [mVaccins removeObject:selectedVac];
            [mVaccinsView reloadData];
            
            [[self mManagedObjectContext] deleteObject:selectedVac];
            
            NSLog(@"Supprimé !!");
        }
    }
    else
    {
        NSAlert * alert = [NSAlert alertWithMessageText:@"Attention"
                                          defaultButton:@"Ok"
                                        alternateButton:nil
                                            otherButton:nil informativeTextWithFormat:[NSString stringWithFormat:@"%@ ne peut pas être supprimé car il est utilisé", [selectedVac Nom]] ];
        
        // should we try again?
        [alert runModal];
    }
    
    
    
}


@end
